package com.yb.bussiness.tribune;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.tribune.Tribune;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;


public class TribuneServiceImpl implements TribuneService {

	private IBaseDao<Tribune, Long> dao;

	private String FIND_SQL ="SELECT * FROM T_P_TRIBUNE where 1=1 ";
	
	public TribuneServiceImpl() {
		dao = new BaseDao<Tribune, Long>(Tribune.class);
	}

	public void delTri(Long id) {
		dao.deleteById(id);
	}

	public Tribune read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(Tribune tri) {
		if(0==tri.getId())
			dao.saveEntity(tri);
		else
			dao.update(tri);
	}

	public List<Tribune> findAll() {
		return dao.findList(FIND_SQL+" ORDER BY lastVisitTime");
	}

	public int getCount(Map<String, String> map) {
		if (map == null || map.size() == 0)
			return 0;
		String FIND_SQL ="SELECT * FROM T_P_TRIBUNE where 1=1 ";
		try {
			if (!"".equals(map.get("startTime")))
				if(!"".equals(map.get("endTime")))
					FIND_SQL += "  AND  CAST(createTime AS DATETIME) BETWEEN CAST('"+map.get("startTime")+"' AS DATETIME) AND CAST('"+map.get("endTime")+"' AS DATETIME)";
				else
					FIND_SQL += "  and createTime>='"+map.get("startTime")+"'";
			else if(!"".equals(map.get("endTime")))
				FIND_SQL += "  and createTime<='"+map.get("endTime")+"'";
			if(!"".equals(map.get("userid")))
				FIND_SQL += "  and authorId = '"+map.get("userid")+"'";
			if(!"".equals(map.get("title")))
				FIND_SQL += "  and title like '%"+map.get("title")+"%'";
			List<Tribune> list = dao.findList(FIND_SQL);
			if (list != null && list.size() > 0)
				return list.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<Tribune> getList(Map<String, String> map, int start,
			int limit) {
		if (map == null || map.size() == 0)
			return null;
		String FIND_SQL ="SELECT * FROM T_P_TRIBUNE where 1=1 ";
		try {
			if (!"".equals(map.get("startTime")))
				if(!"".equals(map.get("endTime")))
					FIND_SQL += "  AND  CAST(createTime AS DATETIME) BETWEEN CAST('"+map.get("startTime")+"' AS DATETIME) AND CAST('"+map.get("endTime")+"' AS DATETIME)";
				else
					FIND_SQL += "  and createTime>='"+map.get("startTime")+"'";
			else if(!"".equals(map.get("endTime")))
				FIND_SQL += "  and createTime<='"+map.get("endTime")+"'";
			if(!"".equals(map.get("userid")))
				FIND_SQL += "  and authorId = '"+map.get("userid")+"'";
			if(!"".equals(map.get("title")))
				FIND_SQL += "  and title like '%"+map.get("title")+"%'";
			List<Tribune> list = dao.findList(FIND_SQL+" ORDER BY CREATETIME desc limit ?,?",new Object[]{(start-1)*limit,limit});
			List<Tribune> newlist = null;
			if (list != null && list.size() > 0) {
				newlist = new ArrayList<Tribune>();
				for (Tribune tp : list) {
					tp.setCreateTimeString(DateTimeUtil.switchDateToString(tp.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tp);
				}
			}
			return newlist;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean deleteById(String id) {
		try {
			if ("".equals(id))
				return false;
			String sql = "delete from t_p_tribune where id=? ";
			dao.delete(sql, new Object[] { id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_p_tribune where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}
	
	public boolean changeIsMarrowById(String id,String isMarrow) {
		try {
			if ("".equals(id))
				return false;
			String sql = "update t_p_tribune set isMarrow=? where id=? ";
			dao.update(sql, new Object[] { id , isMarrow });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean changeIsMarrowByIdString(String idString,String isMarrow) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "update t_p_tribune set isMarrow="+isMarrow+" where id=" + delteid[i];
				dao.update(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}
	
	public Tribune getById(String id) {
		if ("".equals(id))
			return null;
		String sql = FIND_SQL+" and id=? ";
		try {
			List<Tribune> list = dao.findList(sql, new Object[] { id });
			if (null != list && list.size() > 0) {
				Tribune tp = list.get(0);
//				tp.setCreatetimeString(DateTimeUtil.switchDateToString(tp.getCreatetime(), "yyyy-MM-dd HH:mm:ss"));
//				tp.setOverTimeString(DateTimeUtil.switchDateToString(tp.getOverTime(), "yyyy-MM-dd HH:mm:ss"));
				return tp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Tribune> find(int i, int j) {
		FIND_SQL+=" ORDER BY lastVisitTime limit ?,?";
		try {
			List<Tribune> list = dao.findList(FIND_SQL, new Object[] { i,j });
			List<Tribune> newlist = null;
			if (list != null && list.size() > 0) {
				newlist = new ArrayList<Tribune>();
				for (Tribune tp : list) {
					tp.setCreateTimeString(DateTimeUtil.switchDateToString(tp.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
					tp.setLastVisitTimeString(DateTimeUtil.switchDateToString(tp.getLastVisitTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tp);
				}
			}
			return newlist;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public PageUtil loadTie(String type, int start, int limit) {
		PageUtil pu = new PageUtil();
		String sql = "select count(b.id) from t_p_tribune b ";
		/*if("0".equals(type))
			sql+=" order by lastVisitTime desc";*/
		if("1".equals(type))
			sql+=" order by createTime desc";
		if("2".equals(type))
			sql+=" order by viewCount desc";
		if("3".equals(type))
			sql+=" order by flowCount desc";
		if("4".equals(type))
			sql+=" where isMarrow ='1' ";
		try {
			int total = Integer.parseInt((dao.findObjectArryList(sql ).get(0))[0].toString());
			pu.setTotalRecords(total);
		} catch (NumberFormatException e) {
			pu.setTotalRecords(0);
		}
		sql = "select * from t_p_tribune b ";
		if("0".equals(type))
			sql+="ORDER BY isTop desc,toTopTime DESC,createTime desc";
		if("1".equals(type))
			sql+=" order by isTop desc,toTopTime DESC,createTime desc";
		if("2".equals(type))
			sql+=" order by isTop desc,toTopTime DESC,viewCount desc";
		if("3".equals(type))
			sql+=" order by isTop desc,toTopTime DESC,flowCount desc";
		if("4".equals(type))
			sql+=" where isMarrow ='1' order by isTop desc,toTopTime DESC";
		sql+=" limit ?,?";
//		sql = "select b.* from t_p_building b , t_m_proinfo p where b.id=p.proid and p.authorId=? and p.operateType=?  limit "
//				+ start + "," + limit;
		List<Tribune> list = dao.findList(sql, new Object[] { (start-1)*limit, limit });
		for (Tribune tp : list) {
			tp.setCreateTimeString(DateTimeUtil.switchDateToString(tp.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			tp.setLastVisitTimeString(DateTimeUtil.switchDateToString(tp.getLastVisitTime(), "yyyy-MM-dd HH:mm:ss"));
			tp.setToTopTimeString(DateTimeUtil.switchDateToString(tp.getToTopTime(), "yyyy-MM-dd HH:mm:ss"));
		}
		pu.setList(list);
		pu.setPageNo(start);
		pu.setPageSize(limit);
		return pu;
	}

	public boolean toTop(String topType, String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				if("1".equals(topType))
					sql = "update t_p_tribune set isTop="+topType+",toTopTime=now() where id=" + delteid[i];
				else
					sql = "update t_p_tribune set isTop="+topType+",toTopTime= null where id=" + delteid[i];
				dao.update(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

}
